package com.bw.message.server.impl;


import com.bw.common.security.service.TokenService;
import com.bw.domin.Message;
import com.bw.domin.Vo.MessageHistory;
import com.bw.domin.Vo.MessageVo;
import com.bw.domin.Vo.PatientVo;
import com.bw.message.mapper.MessageMapper;
import com.bw.message.server.MessageService;
import com.bw.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class PatientServiceImpl implements MessageService {
    @Autowired
    private MessageMapper patientMapper;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询所有分组及分组下的患者
     * @return
     */
    @Override
    public List<PatientVo> selectGroup() {
        Long doctorId = tokenService.getLoginUser().getUserid();
        List<PatientVo> patientVos = patientMapper.selectGroup(doctorId);
        return patientVos;
    }

    /**
     * 查询群发记录
     * @param messageContent
     * @return
     */
    @Override
    public List<Message> selectMessage(String messageContent) {
        Message message = new Message();
        message.setDoctorId(tokenService.getLoginUser().getUserid());
        message.setMessageContent(messageContent);
        return patientMapper.selectMessage(message);
    }

    /**
     * 查询群发详情
     * @param id
     * @return
     */
    @Override
    public List<MessageHistory> selectMessageDetails(Long id) {
        List<MessageHistory> messageHistories = patientMapper.selectMessageDetails(id);
        return messageHistories;
    }

    /**
     * 查询患者
     * @param groupId
     * @return
     */
    @Override
    public List<PatientVo> selectPatient(Long groupId) {
        List<PatientVo> patientVos = patientMapper.selectPatient(groupId);
        return patientVos;
    }

    /**
     * 新建群发
     * @param messageVo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addMessage(MessageVo messageVo) {
        LoginUser loginUser = tokenService.getLoginUser();
        Long userid = loginUser.getUserid();
        //添加群发消息
        messageVo.setDockerId(userid);
        patientMapper.addMessage(messageVo);
        //添加群发消息用户
        Long id = messageVo.getId();
        List<Long> userIds = messageVo.getUserIds();
        patientMapper.addMessageUser(id,userIds);
    }

    /**
     * 查询群发历史
     * @return
     */
    @Override
    public List<MessageHistory> selectMessageHistory() {
        LoginUser loginUser = tokenService.getLoginUser();
        Long userid = loginUser.getUserid();
        MessageHistory messageHistory = new MessageHistory();
        messageHistory.setDockerId(userid);
        List<MessageHistory> messageHistories = patientMapper.selectMessageHistory(messageHistory);
        return messageHistories;
    }
}
